import { defineStore } from 'pinia';
import piniaPersistConfig from './piniaPersist';
import { Menu } from '@/api/types';
import { getAuthRoute } from '@/api/route';
import { getAllBreadcrumbList, getFlatArr } from '@/utils';

export interface RouteState {
	routeList: Menu.SysRouteVo[];
}

// TabsStore
export const RouteStore = defineStore({
	id: 'RouteState',
	state: (): RouteState => ({
		routeList: [],
	}),
	getters: {
		// 扁平化之后的一维数组路由，主要用来添加动态路由
		getFlatRouteList: (state) => getFlatArr(state.routeList),
		// 所有面包屑导航列表
		getBreadcrumbList: (state) => getAllBreadcrumbList(state.routeList),
	},
	actions: {
		async initRoutesFormServer() {
			const { body } = await getAuthRoute();
			this.routeList = body;
		},
	},
	// persist: piniaPersistConfig('RouteState'),
});
